//
//  MYGMedicalToolsGFRController.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/4/21.
//

import UIKit

class MYGMedicalToolsGFRController: MYGBaseController {
    // MARK: - Property
    private var sex: Int?
    private var race: Int?
    private var age: Int?
    private var area: Float?
    private var creatinine: Float?

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        let separator1 = createSeparator()
        let separator2 = createSeparator()
        let separator3 = createSeparator()
        let separator4 = createSeparator()
        let separator5 = createSeparator()
        let separator6 = createSeparator()
        view.addSubviews([
            gfrTitleLabel,
            gfrTF,
            separator1,
            ageInputView,
            separator2,
            creatinineInputView,
            separator3,
            sexChooseView,
            separator4,
            areaInputView,
            separator5,
            raceChooseView,
            separator6,
            commitButton
        ])
        
        gfrTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(26.5)
            make.left.equalTo(26)
            make.height.equalTo(22.5)
        }
        
        gfrTF.snp.makeConstraints { make in
            make.top.equalTo(gfrTitleLabel.snp.bottom).offset(15)
            make.left.equalTo(26)
            make.right.equalTo(-26)
            make.height.equalTo(22)
        }
        
        separator1.snp.makeConstraints { make in
            make.top.equalTo(gfrTF.snp.bottom).offset(7.5)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        ageInputView.snp.makeConstraints { make in
            make.top.equalTo(separator1.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
      
        separator2.snp.makeConstraints { make in
            make.top.equalTo(ageInputView.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        creatinineInputView.snp.makeConstraints { make in
            make.top.equalTo(separator2.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator3.snp.makeConstraints { make in
            make.top.equalTo(creatinineInputView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        sexChooseView.snp.makeConstraints { make in
            make.top.equalTo(separator3.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator4.snp.makeConstraints { make in
            make.top.equalTo(sexChooseView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        raceChooseView.snp.makeConstraints { make in
            make.top.equalTo(separator4.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
    
        separator5.snp.makeConstraints { make in
            make.top.equalTo(raceChooseView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        areaInputView.snp.makeConstraints { make in
            make.top.equalTo(separator5.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator6.snp.makeConstraints { make in
            make.top.equalTo(areaInputView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
    
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    // MARK: - lazy load
    private lazy var gfrTitleLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(16)
        label.textColor = .rgba(51, 51, 51)
        label.text = "GFR"
        return label
    }()
    
    private lazy var gfrTF: UITextField = {
        let tf = UITextField()
        tf.font = .regular(14)
        tf.textColor = .rgba(51, 51, 51)
        tf.placeholder = "输入数据后，进行计算"
        tf.setPlaceHolderTextColor(.rgba(204, 204, 204))
        tf.isEnabled = false
        return tf
    }()
    
    private lazy var ageInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .age, frame: .zero)
        view.inputDidChange = {[weak self] (age) in
            self?.age = Int(age)
        }
        return view
    }()
    
    private lazy var creatinineInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .custom(title: "血肌酐", unit: "mg/dL", placeholder: "请输入血肌酐", keyboardType: .decimalPad), frame: .zero)
        view.inputDidChange = {[weak self] (creatinine) in
            self?.creatinine = Float(creatinine)
        }
        return view
    }()
    
    private lazy var areaInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .custom(title: "体表面积", unit: "m²", placeholder: "请输入体表面积", keyboardType: .decimalPad), frame: .zero)
        view.inputDidChange = {[weak self] (area) in
            self?.area = Float(area)
        }
        return view
    }()
    
    private lazy var sexChooseView: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "性别"
        view.leftTitle = "男"
        view.rightTitle = "女"
        view.selectedAction = {[weak self] (sex) in
            self?.sex = sex
        }
        return view
    }()
    
    private lazy var raceChooseView: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "种族"
        view.leftTitle = "黑人"
        view.rightTitle = "其他"
        view.selectedAction = {[weak self] (race) in
            self?.race = race
        }
        return view
    }()

    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
    
}

extension MYGMedicalToolsGFRController {
    @objc func commit() {
        view.endEditing(true)
        
        // 体重(kg)×(140—年龄)/72×血清肌酐值(mg/L)
        
        guard let sex = sex, let race = race, let age = age, let area = area, let creatinine = creatinine else { return }
        
        // GFR = 141 x MIN(Scr/k, 1)a x max(src/k, 1)-1.209 x 0.993age x 1.018(女) x 1.159(黑)
        let k :Float = sex == 0 ? 0.9 : 0.7
        let a :Float = sex == 0 ? -0.329 : -0.411
        var gfr = 141 * powf(min(creatinine/k, 1), a) * powf(max(creatinine/k, 1), -1.209) * powf(0.993, Float(age))
        if sex == 1 {
            gfr = gfr * 1.018
        }
        
        if race == 0 {
            gfr = gfr * 1.159
        }
        
        let text = "\(gfr) mL/min" as NSString
        var att = NSMutableAttributedString(string: text as String, attributes: [.font: UIFont.bold(20), .foregroundColor: UIColor.rgba(51, 51, 51)])
        att.setAttributes([.font: UIFont.medium(15), .foregroundColor: UIColor.rgba(102, 102, 102)], range: text.range(of: "mL/min"))
        gfrTF.attributedText = att
    }
}
